﻿using aier.dataaccess.dapper;
using aier.dataaccess.model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aier.business.logic
{
    public class ScreenTeamBll
    {
        public ScreenTeamDal screenTeamDal { get; set; }

        /// <summary>
        /// 获取所有筛查小组
        /// </summary>
        /// <returns></returns>
        public IEnumerable<ScreenTeamView> GetScreenAllTeams()
        {
            return screenTeamDal.GetScreenAllTeams();
        }

        public IEnumerable<ScreenTeamView> GetScreenTeams(Guid ownerId) {
          return  screenTeamDal.GetScreenTeams(ownerId).GroupBy(t=>t.new_screenteamId).Select(t=>t.FirstOrDefault()).OrderByDescending(t=>t.new_screenteamId);
        }

        /// <summary>
        /// 根据筛查小组名称查询当前筛查小组
        /// 原程序使用：selectTeamIdByOwerName
        /// </summary>
        /// <param name="ownername">筛查小组名称</param>
        /// <returns></returns>
        public ScreenTeamView QueryScreenTeamByOwnerName(string ownername)
        {
            return screenTeamDal.QueryScreenTeamByOwnerName(ownername);
        }

        /// <summary>
        /// 根据当前登录用户Id获取筛查小组
        /// </summary>
        /// <param name="ownerid">用户Id</param>
        /// <returns></returns>
        public ScreenTeamView QueryScreenTeamByOwnerId(Guid ownerid)
        {
            return screenTeamDal.QueryScreenTeamByOwnerId(ownerid);
        }

        /// <summary>
        /// 根据筛查小组名称查询当前筛查小组
        /// 原程序使用：getScreenteam
        /// </summary>
        /// <param name="name">筛查小组名称</param>
        /// <param name="hospital">医院ID</param>
        /// <param name="ownerid">负责人ID</param>
        /// <param name="statecode">状态０：可用,２:停用</param>
        /// <returns></returns>
        public IEnumerable<ScreenTeamView> QueryScreenTeam(string name, string hospital, string ownerid, int statecode = 0)
        {
            return screenTeamDal.QueryScreenTeam(name, hospital, ownerid,statecode);
        }

        public dynamic GetScreenTeamMemberByTeamid(string teamid)
        {
            var TeamMember = "";
            var result = screenTeamDal.GetScreenTeamMemberByTeamid(teamid);
            for (int i = 0; i < result.Count; i++)
            {
                if (i != result.Count - 1)
                {
                    TeamMember += result[i].FullName + ",";
                }
                else
                {
                    TeamMember += result[i].FullName;
                }
                
            }
            return TeamMember;

        }
    }
}
